package com.wisdom.web.controllers;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.wisdom.exceptions.UserNotFoundException;
import com.wisdom.model.dao.UserDao;
import com.wisdom.model.entities.User;

import org.springframework.ui.ExtendedModelMap;

@Controller
public class UserController {
	private static final String PREFIX = "/admin/users";
	private String LOGIN_SUCCESS = "index", LOGIN_FAILED = "index", LOGOUT_SUCCESS = "index";
	private String INCORRECT_LOGIN = "Incorrect login", REASON = "reason", INCORRECT_PASSWORD = "Password incorrect", USER="user";
	private static final String ADMIN_INDEX = "admin"; 
	
	private static final String redirectToListAllUsers = "redirect:/admin/users/all";

	public static final String LIST_USERS = "list_users",
							   USER_HAS_BEEN_ADDED = redirectToListAllUsers,
							   USER_HAS_BEEN_REMOVED = redirectToListAllUsers,
							   USER_INFO = "user_info",
							   USER_HAS_NOT_BEEN_FOUND = redirectToListAllUsers,
							   ADD_USER_FORM = "add_user"; 
	@Autowired
	private UserDao userDao;
			
	@RequestMapping(value=PREFIX+"/")
	public String adminIndex(Model model){
		return ADMIN_INDEX; 
	}
	
	@RequestMapping(value=PREFIX + "/all")
	public String getUsers(Model model){
		model.addAttribute("users", userDao.listUsers());
		return LIST_USERS;
	}
	
	@RequestMapping(value=PREFIX+"/{login}")
	public String getUser(@PathVariable String login, Model model){
		try{
			model.addAttribute("user", userDao.getUser(login));
			return USER_INFO;
		}catch(UserNotFoundException ex){
			return USER_HAS_NOT_BEEN_FOUND;
		}
	}
	@RequestMapping(value=PREFIX+"/add_user")
	public String addUser(@RequestParam(required=false) String login, 
						 @RequestParam(required=false) String password, 
						 @RequestParam(required=false) String userName,
						 @RequestParam(required=false) String email,
						 Model model){
		if (login == null || password == null || userName == null)
			return ADD_USER_FORM; 
		
		User user = new User();
		user.setName(userName);
		user.setLogin(login);
		user.setPassword(password);
		user.setEmail(email);
		user.setRegistrationDate(new Date());
		user.setAlive(true);
		user.setEnterDate(new Date());
		userDao.addUser(user);
	
		return USER_HAS_BEEN_ADDED;
	}
	@RequestMapping(value=PREFIX+"/remove_user/{login}")
	public String removeUser(@PathVariable String login, Model model){
		User user;
		try {
			user = userDao.deleteUser(login);
		} catch (UserNotFoundException e) {
			return USER_HAS_NOT_BEEN_FOUND;
		}
		model.addAttribute("user", user);
		
		return USER_HAS_BEEN_REMOVED;
	}
	
	@RequestMapping("/logout")
	public String logout(HttpServletRequest req){
		req.getSession().removeAttribute(USER);
		return LOGOUT_SUCCESS;
	}
	
	@RequestMapping(value="/login")
	public String login(HttpServletRequest request, @RequestParam String login, @RequestParam String password, Model model){
		User user = null;
		try {
			user = userDao.getUser(login);
		} catch (UserNotFoundException e) {
			model.addAttribute(REASON, INCORRECT_LOGIN);
			return LOGIN_FAILED;
		}
		if (user.checkPassword(password)){
			request.getSession().setAttribute(USER, user);
			model.addAttribute(USER, user);
			return LOGIN_SUCCESS; 
		}else{
			model.addAttribute(REASON, INCORRECT_PASSWORD);
			return LOGIN_FAILED;
		}
	}	
}
